package showtime.framework.utils
{
	
	/** 
	 * @project showtime
	 * @author yinhao
	 * @date 2011-3-1
	 *
	 */	
	public class TriggerBean
	{
		private var defaultFactor:int = 0;
		
		private var now:int = 0;
		
		private var triggerFactory:int = 0;
		
		private var randomFactor:int = 0;
		
		private var _callback:Function = null;
		
		/**
		 * Default constructor. 
		 * 
		 */		
		public function TriggerBean(callback:Function, factor:int = 1, random:int = 0)
		{
			_callback = callback
				
			defaultFactor = factor;
			
			randomFactor = random;
			
			reset();
		}
		
		public function execute():void
		{

			now++;
			
			if (now == triggerFactory && _callback != null)
			{
				_callback();
				
				reset();
			}
		}
		
		public function reset():void
		{
			now = 0;
			
			triggerFactory = Math.random() * randomFactor + defaultFactor;
		}
	}
}